package com.edgar;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import org.joda.time.DateTime;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

public class SystemController extends Controller {

    public void index() {
    }


    public void zoneList() {
        int page = getParaToInt(Contants.DATATABLE_PAGINATE_OFFSET, Contants.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(Contants.DATATABLE_PAGINATE_LIMIT, Contants.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        FileZone fileZone = getModel(FileZone.class);
        Page<FileZone> fileZonePage = fileZone.paginate(page, rows, "select * ", "from t_file_zone");

        Ret ret = Ret.create();
        ret.put(Contants.DATATABLE_PAGINATE_TOTAL, fileZonePage.getTotalRow());
        ret.put(Contants.DATATABLE_PAGINATE_ROWS, fileZonePage.getList());

        renderJson(ret);
    }

    public void zoneAdd() {
        render("zoneForm.html");
    }

    public void zoneSave() {

        Ret ret = Ret.create();
        String[] uploadFileList = getParaValues("uploadFiles");

        FileZone fileZone = getModel(FileZone.class);
        DateTime dateTime = DateTime.now();
        fileZone.set(FileZone.SETTING_TIME, dateTime.toString(Contants.DEFAULT_DATE_FOMATTER));
        fileZone.set(FileZone.ZONE_ID, Identities.uuid2());
        fileZone.set(FileZone.IS_FINISH, 0);
        fileZone.set(FileZone.DELETE_TIME, dateTime.plusDays(Integer.parseInt(fileZone.getStr(FileZone.RETAIN_DAYS))).toString(Contants.DEFAULT_DATE_FOMATTER));

        //判断文件是否存在
        for (String uploadFile : uploadFileList) {
            String directory = fileZone.get(FileZone.DIRECTORY);
            if (directory.contains("\\")) {
                directory = directory.replace("\\", "/");
            }
            File file = new File(directory + "/" + uploadFile);
            if (!file.exists()) {
                ret.put(Contants.RESPONSE_RESULT_FLAG, false);
                ret.put(Contants.RESPONSE_RESULT_MESSAGE, uploadFile + ",文件不存在");
                renderJson(ret);
                return;
            }
        }

        boolean flag = Db.tx(new IAtom() {
            @Override
            public boolean run() throws SQLException {
                boolean isSave = fileZone.save();
                if (!isSave) return false;


                for (String uploadFile : uploadFileList) {
                    FileBox fileBox = new FileBox();
                    fileBox.set(FileBox.FILE_ID, Identities.uuid2());
                    fileBox.set(FileZone.ZONE_ID, fileZone.getStr(FileZone.ZONE_ID));
                    String directory = fileZone.get(FileZone.DIRECTORY);
                    if (directory.contains("\\")) {
                        directory = directory.replace("\\", "/");
                    }
                    fileBox.set(FileBox.URL, directory + "/" + uploadFile);
                    fileBox.set(FileBox.IS_DELETE, 0);
                    fileBox.set(FileBox.CREATETS, DateTime.now().toString(Contants.DEFAULT_DATE_FOMATTER));
                    boolean isFileBoxSave = fileBox.save();
                    if (!isFileBoxSave) return false;
                }

                return true;
            }
        });


        ret.put(Contants.RESPONSE_RESULT_FLAG, flag);

        if (flag) {
            ret.put(Contants.RESPONSE_RESULT_MESSAGE, "增加区域成功!");
        } else {
            ret.put(Contants.RESPONSE_RESULT_MESSAGE, "增加区域失败!");
        }

        renderJson(ret);
    }

    public void zoneRemove() {
        String ids = getPara();
        String[] idArr = ids.split(",");

        boolean flag = Db.tx(new IAtom() {
            @Override
            public boolean run() throws SQLException {
                for (String zoneId : idArr) {
                    boolean isDelete = FileZone.dao.deleteById(zoneId);
                    if (!isDelete) return false;

                    boolean isFileBoxDelete = FileBox.dao.deleteByZoneId(zoneId);
                    if (!isFileBoxDelete) return false;
                }
                return true;
            }
        });

        Ret ret = Ret.create();
        ret.put(Contants.RESPONSE_RESULT_FLAG, flag);

        if (flag) {
            ret.put(Contants.RESPONSE_RESULT_MESSAGE, "删除区域成功!");
        } else {
            ret.put(Contants.RESPONSE_RESULT_MESSAGE, "删除区域失败!");
        }

        renderJson(ret);
    }


    public void fileBox() {
        String zoneId = getPara();
        FileZone fileZone = FileZone.dao.findById(zoneId);
        setAttr("fileZone", fileZone);
    }

    public void fileBoxList() {

        int page = getParaToInt(Contants.DATATABLE_PAGINATE_OFFSET, Contants.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(Contants.DATATABLE_PAGINATE_LIMIT, Contants.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        FileBox fileBox = getModel(FileBox.class);
        Page<FileBox> fileZonePage = fileBox.paginateList(fileBox, page, rows);

        Ret ret = Ret.create();
        ret.put(Contants.DATATABLE_PAGINATE_TOTAL, fileZonePage.getTotalRow());
        ret.put(Contants.DATATABLE_PAGINATE_ROWS, fileZonePage.getList());

        renderJson(ret);

    }

    public void fileBoxRemove() {
        String ids = getPara();
        String[] idArr = ids.split(",");

        boolean flag = Db.tx(new IAtom() {
            @Override
            public boolean run() throws SQLException {
                for (String fileId : idArr) {

                    boolean isFileBoxDelete = FileBox.dao.deleteById(fileId);
                    if (!isFileBoxDelete) return false;
                }
                return true;
            }
        });

        Ret ret = Ret.create();
        ret.put(Contants.RESPONSE_RESULT_FLAG, flag);

        if (flag) {
            ret.put(Contants.RESPONSE_RESULT_MESSAGE, "删除文件成功!");
        } else {
            ret.put(Contants.RESPONSE_RESULT_MESSAGE, "删除文件失败!");
        }

        renderJson(ret);
    }
}
